package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessPlan;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.PlantTempVO;
import com.digitalchina.lxnsy.entity.ProductStandardVO;
import com.digitalchina.lxnsy.toolkit.ActivityTaskManager;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.xml.UserXML;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2019/1/8 17:14
 * 修改备注：
 * ================================================
 */
public class SelectProductionDetailActivity extends BaseActivity {

    @Bind(R.id.web_view)
    WebView webView;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_price)
    TextView tvPrice;
    @Bind(R.id.tv_next)
    TextView tvNext;
    private ProductStandardVO vo;

    private int nextType = 0; // 0已购买  1免费  2付费
    private boolean isShare = false; // 是否有资格购买

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_select_production_detail);
        setTitle("标准化生产预览");
    }

    @Override
    public void initMember() {
        vo = (ProductStandardVO) getIntent().getSerializableExtra("ProductStandardVO");
        webView.loadUrl(vo.getStandardSynopsisUrl() + "&userId=" + UserXML.getUserId(this));

        //  购买标准状态 0有效 1无效
        if (vo.getValidityStatus() == 0) {
            tvName.setVisibility(View.GONE);
            tvPrice.setText("已购买");
            tvNext.setText("下一步");
            nextType = 0;
        } else {
            // 0免费  1付费
            if (vo.getFeeType() == 0) {
                tvName.setVisibility(View.GONE);
                tvPrice.setTextSize(14);
                tvPrice.setText("分享至朋友圈免费使用");
                tvNext.setText("下一步");
                nextType = 1;
            } else {
                tvName.setVisibility(View.VISIBLE);
                tvPrice.setText("￥" + vo.getFee());
                tvNext.setText("立即购买");
                nextType = 2;
            }
        }

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_PLACE_ORDER_SUCCESS: // 免费的下单成功
                        ActivityTaskManager.getInstance().closeActivity(SelectProductionActivityV2.class);
                        GotoUtil.gotoActivity(SelectProductionDetailActivity.this, AddStandardProductionActivityV2.class, true, "ProductStandardVO", vo);
                        break;
                    case MSG.MSG_PLACE_ORDER_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.tv_next)
    public void onViewClicked() {
        switch (nextType) {
            case 0:
                GotoUtil.gotoActivity(SelectProductionDetailActivity.this, AddStandardProductionActivityV2.class, true, "ProductStandardVO", vo);
                break;
            case 1:
                    BusinessPlan.placeOrder(SelectProductionDetailActivity.this, vo.getId(), "0", "1", mHandler);

                break;
            case 2:
                PlantTempVO vo1 = new PlantTempVO();
                vo1.setPlantId(vo.getId());
                vo1.setName(vo.getName());
                vo1.setType("3");
                GotoUtil.gotoActivity(SelectProductionDetailActivity.this, PlantPayActivity.class, true, "PlantTempVO", vo1);
                break;
        }
    }


}
